<?php
error_reporting(0);
header("Content-type: text/html; charset=utf-8");
session_start();

require_once 'common/config.php';
require_once 'common/conn.php';
require_once 'common/function.php';

require_once 'db/account.db.php';
require_once 'db/special.db.php';
require_once 'db/itemType.db.php';
require_once 'db/accountMoney.db.php';
require_once 'db/friend.db.php';
require_once 'db/character.db.php';
require_once 'common/common.php';


//echo $_SESSION['AccUnique'].'-'.$_SESSION['AccountID'].'-'.$_SESSION['AccountPass'];
//sprintf(param,"user_id=%s&user_pw=%s", g_BravoMain.m_cLoginID, g_BravoMain.m_cLoginPassMD5);//,pMsg->m_byErrStr, pMsg->m_byPhoneNumber);
//echo md5("123456");
$job = isset($_GET['job']) ? strval($_GET['job']) : "";//玩家职业
$alljob = isset($_GET['alljob']) ? strval($_GET['alljob']) : "";//展示所有 默认0 不展示全部 1展示全部
$accountId = $_SESSION['AccountID'];
$accountPass = $_SESSION['AccountPass'];
if( $accountId == "" || $accountPass == "")
{
    leError("账号信息获取失败，请重新打开商城");
    return;
}

if ($job == "") {
	$job = isset($_POST['job']) ? strval($_POST['job']) : "";
}
if($alljob == 1 )
{
	$xjob =-1;
}else
{
	//echo $job.'<br />';
	if($job != 0 && $job != 4 )
	{
		$yyjob = getAlljob($job);
		$xjob = $yyjob;
	}else
	{
		$xjob = $job;
	}
	//$xjob = $yyjob;
	//echo $xjob.'aaaaaa';
	//var_dump($xjob);
}
/*
  $key = isset($_GET['key']) ? strval($_GET['key']) : "";
  if ($key == "") {
  $key = isset($_POST['key']) ? strval($_POST['key']) : "";
  }

  if ($key != "Glmall0119") {
  return;
  }
 */


/*
  $paperDB = "paper3";
  $tgameDB = "BBGame_Test";
 */

$paperDB = "paper";
$tgameDB = "TGame_Test";
/*
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); //将出错信息输出到一个文本文件
 */

$dbc = new DBC();
$balance = 0; //余额

$accountDb = new AccountDb($dbc);
$user = $accountDb->getAccountByAccountId($accountId);

if (isArrayNotEmpty($user)) {
    $pass = trim($user[0]['AccountPass']);
    $md5 = md5($pass);
    //echo " accountPass=[".$accountPass."]   pass=[".$pass."]  md5=[".$md5."]";
    if ($accountPass != $pass) {
        return;
    }
    $_SESSION['AccUnique'] = $user[0]['AccUnique'];
    $_SESSION['AccountID'] = $user[0]['AccountID'];
    if (isset($_SESSION['AccountID']) && $_SESSION['AccountID'] != "") {
        $accountMoneyDb = new AccountMoneyDb($dbc);
        $accountMoney = $accountMoneyDb->getAccountMoney($_SESSION['AccUnique']);
        if ($accountMoney && isArrayNotEmpty($accountMoney)) {
            $balance = $accountMoney[0]['Balance'];
        }
    }
}

$itemDb = new SpecialDb($dbc);

$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$adjacents = isset($_GET['adjacents']) ? intval($_GET['adjacents']) : 0;

if ($page <= 0) {
    $page = 1;
}
if ($adjacents <= 0) {
    $adjacents = 4;
}

$limit = 50;
$start = ($page - 1) * $limit;


if (isset($_GET['type'])) {
    $type = intval($_GET['type']);
} else {
    $type = 5;
}

$data = $itemDb->getItem($type, $start, $limit);

$itemTypeDb = new ItemTypeDb($dbc);
$itemTypeArray = $itemTypeDb->getItemType();

$numberOfObject = $itemDb->getItemNumber($type);

$dbc2 = new DBC($paperDB);
$friendDb = new FriendDb($dbc2); //file_put_contents("txt2.txt","11111111111111111111111111");
$friends = $friendDb->getFriendByAccount($_SESSION['AccUnique']);
//file_put_contents("txt.txt", json_encode($friends));

$friends_count = $friends ? count($friends) : 0;
$dbc3 = new DBC($tgameDB);
$characterDb = new CharacterDb($dbc3);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title><?php echo TITLE; ?></title>
        <link rel="stylesheet" href="./css/main_special.css">
        <!--[if lt IE 9]>
        <script src="js/html5shiv.min.js"></script>
        <script src="js/respond.min.js"></script>
        <![endif]-->
        <style>
            .yuanjiao{
                border-radius: 3px;
               /*behavior: url(PIE.htc);*/
                position: relative;
                z-index: 2;
            }
            .product_item .product_introl .product_props>a{
                border-radius: 3px;
                /*behavior: url(PIE.htc);*/
                position: relative;
                z-index: 2;
            }
            .product_item .product_introl .product_ctrl>a{
                border:1px solid #333;
                border-radius: 3px;
               /*behavior: url(PIE.htc);*/
                position: relative;
                z-index: 2;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="product_nav">
                <div class="dianshu yuanjiao">
                    <span class="dianshu_num">荣币：<span id="balance_show"><?php echo $balance; ?></span></span>
                    <!--<a href="http://new.shoukabao.cn/Payment/Service/ab68f07b19732d558922c58b00b36545" target="_blank" class="chongzhi_btn">在线充值</a>-->
					<a href="javascript:void(0)" class="chongzhi_btn yuanjiao" onclick="testgo();">在线充值</a>
                </div>
                <a href="store.php?type=1&job=<?php echo $job;  ?>&alljob=<?php echo $alljob; ?>" class="nav_btn zuixin yuanjiao"><i class="nav_icon"></i>最新商品</a>
                <a href="store.php?type=2&job=<?php echo $job;  ?>&alljob=<?php echo $alljob; ?>" class="nav_btn shizhuang yuanjiao"><i class="nav_icon"></i>角色时装</a>
                <a href="store.php?&type=3&job=<?php echo $job;  ?>&alljob=<?php echo $alljob; ?>" class="nav_btn zuoqi yuanjiao"><i class="nav_icon"></i>坐骑装备</a>
                <a href="store.php?type=4&job=<?php echo $job;  ?>&alljob=<?php echo $alljob; ?>" class="nav_btn daoju yuanjiao"><i class="nav_icon"></i>功能道具</a>
                <a href="special.php?type=5&job=<?php echo $job;  ?>&alljob=<?php echo $alljob; ?>" class="nav_btn meiri yuanjiao"><i class="nav_icon"></i>每日专区</a>
                <a href="Point.php?type=6&job=<?php echo $job;  ?>&alljob=<?php echo $alljob; ?>" class="nav_btn jifen yuanjiao"><i class="nav_icon"></i>积分专区</a>
            </div>
            <div class="product_list">
                <?php
                //$imageUrl = "http://103.45.101.157:81/RYweb/";
				$imageUrl = "http://itemmall.qx680.com:8888/store/";
                for ($i = 0; $i < count($data); $i++) {
                    $name = trim($data[$i]['ItemName']);
                    $time_jieshao = trim($data[$i]['time_jieshao']);
                    $alt = trim($data[$i]['alt']);
                    $name = mb_convert_encoding($name, "UTF-8", "GBK");
                    $time_jieshao = iconv("GBK", "UTF-8", $time_jieshao);
                    $alt = iconv("GBK", "UTF-8", $alt);
                    ?>
                    <div class="product_item">
                        <div class="product_img_box">
                            <img id="image_<?php echo $i; ?>" src="<?php echo $imageUrl . $data[$i]['ImageUrl']; ?>" title="<?php echo $alt; ?>" alt="<?php echo $alt; ?>">
                            <input type="hidden" id="item_<?php echo $i; ?>" value="<?php echo $data[$i]['ItemNo']; ?>" />
                            <input type="hidden" id="idx_<?php echo $i; ?>" value="<?php echo $data[$i]['id_x']; ?>" />
                            <input type="hidden" id="alt_<?php echo $i; ?>" value="<?php echo $alt; ?>" />
                        </div>
                        <div class="product_introl">
                            <div class="detail_introl yuanjiao" id="name_<?php echo $i; ?>"><?php echo $name; ?></div>
                            <div class="product_props">
                                <a href="javascript:void(0)" id="jieshao_<?php echo $i; ?>">剩余<?php echo $data[$i]['total_amount']; ?>件</a>
                                <a href="javascript:void(0)"><span id="price_<?php echo $i; ?>"><?php echo $data[$i]['Price']; ?></span>荣币</a>
                            </div>
                            <div class="product_ctrl">
                                <a href="javascript:void(0)"><span>每日限购<?php echo $data[$i]['total_perday']; ?>件</span></a>
                                <a href="javascript:void(0)" class="buy" style="width:43%;" onclick="buy_sdj(<?php echo $i; ?>)">购买</a>
                            </div>
                        </div>
                    </div>
                <?php } ?>

            </div>
        </div>
        <div class="div_overlay" id="modal_div">
            <div id="popupdiv"  class="popupdiv zengsong">
                <div class="dialog_body">
                    <div class="dialog_header">
                        <p class="title_link"><img src="./images/arrow.png" alt="">购买道具</p>
                        <span class="close_key"><img src="./images/close_caret.png" alt="" onclick="close_div()"></span>
                    </div>
                    <div class="dialog_content">
                        <div class="product_detail">
                            <div class="product_img_wrapper">
                                <img class="product_img" id="current_image" src="" alt=""><!--这个是商品的渲染图片标签-->
                                <input type="hidden" id="current_item" value="" />
                                <input type="hidden" id="current_idx" value="" />
                                <input type="hidden" id="current_price" value="" />
                            </div>
                            <div class="detail_introl_wrapper">
                                <div><span clas="name_label">名称</span><span class="name_value" id="current_name"></span></div>
                                <div class="detail_introl_txt" id="current_alt">
                                </div>
                            </div>
                        </div>
                        <div class="btn_group">
                            <a href="javascript:void(0)" class="show_day"><span id="current_jieshao"></span><span id="show_price"></span> 荣币</a>
                            <a href="javascript:void(0)" class="cancel" onclick="close_div()">取消</a>
                            <a href="javascript:void(0)" class="confirm" onclick="confirm_buy()" id="confirm_button">确认赠送</a>
                        </div>
                    </div>
                </div>
                <div class="friends_list" id="select_div">
                    <div class="friends_list_header">
                        <img src="./images/user.png" alt="">赠送好友
                    </div>
                    <ul class="friends_list_ul">
                        <!--<li onselect="selectli(this)" onclick="">ss</li>-->
                        <?php
                        for ($i = 0, $len = count($friends); $i < $len; $i++) {
                            // $friends[$i]['FriendName'] = mb_convert_encoding(trim($friends[$i]['FriendName']), "UTF-8", "GBK");
                            $name = trim($friends[$i]['FriendName']);
                            $characters = $characterDb->getCharacterByName($name);
                            if (count($characters) > 0) {
                                $name = mb_convert_encoding($name, "UTF-8", "GBK");
                                //echo "<option value='" . $characters[0]['AccountUnique'] . "'>$name</option>";
                                echo "<li  onclick=\"selectli(this)\" onselect=\"selectli(this)\" id=\"" . $characters[0]['AccountUnique'] . "\">" . $name . "</li>";
                            }
                        }
                        ?>
                    </ul>
                </div>
            </div>
        </div>
        <!--
        <div class="div_overlay" id="modal_div">
            <div class="product_item" style="margin: 25% 25%; background-color: wheat; opacity: 1.0;filter:Alpha(opacity=100);">
                <div class="product_img_box" style="margin: 20px;">
                    <img  id="current_image" src="" alt="">
                    <input type="hidden" id="current_item" value="" />
                    <input type="hidden" id="current_idx" value="" />
                </div>
                <div class="product_introl">
                    <div class="detail_introl" id="current_name"><?php echo $name; ?></div>
                    <div class="product_props">
                        <a href="javascript:void(0)">半年</a>
                        <a href="javascript:void(0)" id="current_price">点数</a>
                    </div>
                    <div class="product_ctrl">
                        <a href="javascript:void(0)" class="give" onclick="close_div()">取消</a>
                        <a href="javascript:void(0)" class="buy" onclick="confirm_buy()">确定</a>
                    </div>
                </div>
                <div id="select_div" class="product_introl">
                    <select id="friend_id" size="10" style="width:200px;">
        <?php
        for ($i = 0, $len = count($friends); $i < $len; $i++) {
            // $friends[$i]['FriendName'] = mb_convert_encoding(trim($friends[$i]['FriendName']), "UTF-8", "GBK");
            $name = trim($friends[$i]['FriendName']);
            $characters = $characterDb->getCharacterByName($name);
            if (count($characters) > 0) {
                $name = mb_convert_encoding($name, "UTF-8", "GBK");
                // echo "<option value='" . $characters[0]['AccountUnique'] . "'>$name</option>";
            }
        }
        ?>
                    </select>
                </div>
            </div>
        </div>
        -->
    </body>
    <script src="./js/jquery-1.11.3.js"></script>
    <script src="./js/layer.js"></script>
    <script>
                                $(function () {
                                    $(document).ready(function () {
                                        $(document).bind("contextmenu", function (e) {
                                            return false;
                                        });
                                    });
                                    $('.product_list>.product_item:nth-child(3n+1)').css({
                                        //   marginLeft: '0'
                                    });
                                });
    </script>
    <script>
        function testgo() {
            layer.msg('平台原因,充值系统维护,需充值联系管理员QQ:719079746', {icon: 2, time: 500000, offset: ['150px', '300px'], btn: ['确认'], btnAlign: 'c'});
            //layer.msg('你赠送的道具已发送至朋友的右下角 MY ITEM，请提醒他查收',5);
        }
    </script>
    <!--<script type="text/javascript" src="js/jquery.js"></script>-->
    <script type="text/javascript">
        var click_status = true;//防止重复点击
        var buy = true;
        var out_index = -1;
        function buy_sdj(index) {
            $(".popupdiv").removeClass("zengsong").addClass("goumai");
            document.getElementById("modal_div").style.display = "block";
            document.getElementById("select_div").style.display = "none";
            document.getElementById("current_image").src = document.getElementById("image_" + index).src;
            document.getElementById("current_name").innerHTML = document.getElementById("name_" + index).innerHTML;
            document.getElementById("show_price").innerHTML = "售价 : " + document.getElementById("price_" + index).innerHTML;
            document.getElementById("current_price").value = document.getElementById("price_" + index).innerHTML;
            document.getElementById("current_item").value = document.getElementById("item_" + index).value;
            document.getElementById("current_idx").value = document.getElementById("idx_" + index).value;
            //document.getElementById("current_jieshao").innerHTML = document.getElementById("jieshao_" + index).innerHTML;
            document.getElementById("current_alt").innerHTML = document.getElementById("alt_" + index).value;
            document.getElementById("confirm_button").innerHTML = "确定购买";
            buy = true;
            out_index = index;
        }
        var haopengyou = "";
        function confirm_buy() {
            var price = document.getElementById("current_price").value;
            var itemNo = document.getElementById("current_item").value;
            var id_x = document.getElementById("current_idx").value;
            if (buy) {
                haopengyou = "";
            } else {
//                var oSelect = document.getElementById('friend_id');
//                var index = oSelect.selectedIndex;   //option数组返回的同时会返回当前option的选中下标selectedIndex
//                var haopengyou = oSelect[index].value;
            }
            if( !click_status )//防止重复点击
            {
                //console.log(1);
                return;
            }
            click_status = false;
            close_div();
            loadding = layer.msg('处理中...', {
                icon: 16
                ,shade: [0.8, '#393D49'],
                offset: ['150px', '300px']
            });
            $.ajax({
                type: "POST",
                url: "api/cart_special.php",
                dataType: "json",
                data: {
                    action: 'confirmBuy',
                    price: price,
                    itemNo: itemNo,
                    id_x: id_x,
                    buy: buy,
                    haoyou: haopengyou
                },
                success: function (data) {
                    click_status = true;//放开点击
                    if (data.code == 0) {
                        if (buy) {
//                            alert("购买成功");
                            //layer.msg('购买成功,请从右下角MY ITEM提取', 5);
                            layer.msg('您购买的道具已发送至右下角 MY ITEM', {icon: 1, time: 500000, offset: ['150px', '300px'], btn: ['知道了'], btnAlign: 'c'});
                            document.getElementById("jieshao_" + out_index).innerHTML = "剩余" + data.left + "件";
                        } else {
//                            alert("赠送成功");
                            //layer.msg('赠送成功', 5);
                            layer.msg('赠送成功,已发送至好友MY ITEM', {icon: 1, time: 500000, offset: ['150px', '300px'], btn: ['知道了'], btnAlign: 'c'});
                        }
                        document.getElementById("modal_div").style.display = "none";
                        document.getElementById("balance_show").innerHTML = data.balance;

                    } else {
//                        alert(data.msg);
                        layer.msg(data.msg, {icon: 2, time: 500000, offset: ['150px', '300px'], btn: ['知道了'], btnAlign: 'c'});
                        document.getElementById("modal_div").style.display = "none";
                    }
                },
                error: function (error, ab, cd) {
                    console.log(error);
                }
            });
        }
        function gift_send(index) {
<?php if ($friends_count > 0) { ?>
                $(".popupdiv").removeClass("goumai").addClass("zengsong");
                document.getElementById("modal_div").style.display = "block";
                document.getElementById("select_div").style.display = "block";
                document.getElementById("current_image").src = document.getElementById("image_" + index).src;
                document.getElementById("current_name").innerHTML = document.getElementById("name_" + index).innerHTML;
                document.getElementById("show_price").innerHTML = "售价 : " + document.getElementById("price_" + index).innerHTML;
                document.getElementById("current_price").value = document.getElementById("price_" + index).innerHTML;
                document.getElementById("current_item").value = document.getElementById("item_" + index).value;
                document.getElementById("current_idx").value = document.getElementById("idx_" + index).value;
                //document.getElementById("current_jieshao").innerHTML = document.getElementById("jieshao_" + index).innerHTML;
                document.getElementById("current_alt").innerHTML = document.getElementById("alt_" + index).value;
                document.getElementById("confirm_button").innerHTML = "确定赠送";
                buy = false;
<?php } else { ?>
                layer.msg('你暂时没有好友', {icon: 2, time: 500000, offset: ['150px', '300px'], btn: ['知道了'], btnAlign: 'c'});
<?php } ?>
            /*
             layer.msg("即将开通，敬请期待", 5);*/
        }
        function close_div() {
            document.getElementById("modal_div").style.display = "none";
        }

        function selectli(e) {
            var lis = document.getElementsByTagName("li");
            for (var i = 0; i < lis.length; i++) {
                lis[i].className = "";
            }
            e.className = "selected";
            haopengyou = e.id;
        }

    </script>
</html>